﻿Public Class CDBKeyMana
    Private Shared mobjCipher As CCipher

    Shared Sub New()
        mobjCipher = New CCipher
    End Sub

    Public Shared Function compress(ByVal userID As String, ByVal pwd As String) As String
        Dim str1 As String, str2 As String
        str1 = "UserID=" & userID & ";" _
            & "PWD=" & pwd & ";"

        '压缩1
        str2 = mobjCipher.token_DataCompress(str1, True)
        '压缩2
        str2 = mobjCipher.token_DataCompress(str2, True)

        compress = str2
    End Function


    Public Shared Function unCompress(ByVal mainStr As String, _
            ByRef userID As String, ByRef pwd As String) As Boolean
        Dim str1 As String, str2 As String
        Dim pos1 As Integer, pos2 As Integer
        On Error GoTo err_1
        str1 = mobjCipher.token_DataCompress(mainStr, False)
        str1 = mobjCipher.token_DataCompress(str1, False)

        '1、帐号
        pos1 = InStr(1, str1, "UserID=")
        If pos1 = 0 Then GoTo err_1
        pos1 = pos1 + 7

        pos2 = InStr(pos1, str1, ";")
        If pos2 = 0 Then GoTo err_1

        userID = Mid(str1, pos1, pos2 - pos1)

        '2、密码
        pos1 = InStr(1, str1, "PWD=")
        If pos1 = 0 Then GoTo err_1
        pos1 = pos1 + 4

        pos2 = InStr(pos1, str1, ";")
        If pos2 = 0 Then GoTo err_1

        pwd = Mid(str1, pos1, pos2 - pos1)

        unCompress = True
        Exit Function
err_1:

    End Function

    ''' <summary>
    ''' 外部调用：加密、解密（String --> String）
    ''' </summary>
    ''' <param name="originStr"></param>
    ''' <param name="isComPress">true-> 加密  false-> 解密</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DataCompress(ByVal originStr As String, ByVal isComPress As Boolean) As String
        Return mobjCipher.token_DataCompress(originStr, isComPress)
    End Function
End Class
